﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SPRelinkDocuments
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRelink_Click(object sender, EventArgs e)
        {
            if (RelinkDocuments())
            {
                lblMessage.Text = "Success!!!";
            }
            else
            {
                lblMessage.Text = "Failure";
            }
        }

        private bool RelinkDocuments()
        {
            try
            {
                SPSite site = new SPSite(txtSiteUrl.Text.Trim());
                SPWeb spWeb = site.OpenWeb();
                SPList spList = spWeb.Lists[new Guid(txtListGuid.Text.Trim())];
                SPDocumentLibrary doclib = (SPDocumentLibrary)spList;
                string solutionUrl = SPHttpUtility.UrlPathEncode(spWeb.Url + "/" + doclib.DocumentTemplateUrl, true);

                foreach (SPListItem spItem in doclib.Items) //loop through the list items
                {
                    SPFieldCollection spFields = spItem.Fields;

                    foreach (SPField spField in spFields) //loop through fields resetting them
                    {
                        if (spField.Type != SPFieldType.Computed &&
                            spField.Type != SPFieldType.Invalid && !spField.ReadOnlyField)
                        {
                            try
                            {
                                spItem[spField.InternalName] = spItem[spField.InternalName];
                            }
                            catch (Exception e)
                            {
                            }
                        }
                    }

                    SPContentType spContentType = spList.ContentTypes[(SPContentTypeId)spItem["ContentTypeId"]];
                    if (spContentType != null) //try to update the item with the content type's template url which is the updated one
                    {
                        if (spContentType.DocumentTemplate.StartsWith("http://") ||
                        spContentType.DocumentTemplate.StartsWith("https://"))
                        {
                            spItem["TemplateUrl"] = SPHttpUtility.UrlPathEncode(spContentType.DocumentTemplate, true);
                        }
                        else if (spContentType.DocumentTemplate.StartsWith("/"))
                        {
                            spItem["TemplateUrl"] = SPHttpUtility.UrlPathEncode(spWeb.Site.MakeFullUrl(spContentType.DocumentTemplate), true);
                        }
                        else
                        {
                            spItem["TemplateUrl"] = SPHttpUtility.UrlPathEncode(spWeb.Url + '/' + spList.RootFolder + '/' + spContentType.DocumentTemplate, true);
                        }
                    }
                    else //no content type found so default template url to solution Url
                    {
                        spItem["TemplateUrl"] = solutionUrl;
                    }
                    spItem.Update();
                    
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
